layui.use(['table', 'form', 'jquery','drawer','layer'], function() {
    let  table = layui.table;
    let  form = layui.form;
    let  $ = layui.jquery;
    let  drawer = layui.drawer;
    let  layer = layui.layer;

    let  MODULE_PATH = "/admin.php/";

    let  cols = [
        [{
                title: '编号',
                field: 'id',
                width: '5%',
                align: 'center'
            },
            {
                title: '校区名',
                field: 'name',
                width: '10%',
                align: 'center'
            },
            {
                title: '电话',
                field: 'phone',
                width: '10%',
                align: 'center'
            },
//            {
//                title: '密钥',
//                field: 'password',
//                width: '10%',
//                align: 'center'
//            },
            {
                title: '地址',
                field: 'address',
                align: 'center'
            },
            {
                title: '操作',
                toolbar: '#organization-bar',
                width: '10%',
                align: 'center'
            }
        ]
    ]

    //数据对象
    let  tableData;

    //数据加载
    table.render({
        id:"campus_table",
        elem: '#campus-table',
        url: MODULE_PATH + 'basedata.campus/index',
        cols: cols,
        skin: 'line',
        toolbar: '#campus-toolbar',
        loading: true,
        text: {
            none : '空空如也'
        },
        response: {
            statusCode: 200,
        },
        page: {
            limit : 10,
            limits : [10,15,20,25,30],
            groups : 3,
        },
        done: function(res,curr,count) {
            tableData = table.cache.campus_table;
            //定义验证
            let  validate = ['',null,0,undefined,'NAN',[]];
            var that = this.elem.next();
            var data = res.data;
            if (data) {
                data.forEach((item,index) => {
                    var colorId = localStorage.getItem("theme-color")
                    if (index % 2 == 0) {
                        tr = that.find('.layui-table-box tbody tr[data-index='+ index +']').css('background-color',colourDeal(colorId));
                    } else {
                        tr = that.find('.layui-table-box tbody tr[data-index='+ index +']').css('background-color');
                    }
                });
            }
            //判断表格中是否存在数据
            if ( (validate.indexOf(data) >= 0) && curr != 1 ) {
                var cr = curr > 1 ? ((curr * 1) - 1) : 1;
                //重载
                table.reload('campus_table',{
                    page : {
                        curr : cr
                    }
                });
            }
        },
        defaultToolbar: [{
            title: '刷新',
            layEvent: 'refresh',
            icon: 'layui-icon-refresh',
        }, 'filter']
    });
    /*=========================== 按钮事件监听事件 ===========================*/
    //tool中的按钮
    table.on('tool(campus-table)', function(obj) {
        if (obj.event === 'remove') {
            window.remove(obj);
        } else if (obj.event === 'edit') {
            window.edit(obj);
        }
    });

    //在toolbar中的按钮
    table.on('toolbar(campus-table)', function(obj) {
        if (obj.event === 'add') {
            window.add();
        } else if (obj.event === 'refresh') {
            window.refresh();
        } else if (obj.event === 'batchRemove') {
            window.batchRemove(obj);
        }
    });

    /*=========================== form表单的点击事件监听 ===========================*/

    //状态修改
    form.on('switch(campus-enable)', function(obj) {
        //根据业务判断是开启还是关闭
        var state = obj.elem.checked == true ? 1 : 0;
        //获取索引
        var index  = obj.othis.parents('tr').attr("data-index");
        //获取id
        var id = tableData[index].id;
        //数据获取
        var data = {
            'id' : id,
            'status' : state
        };
        //发送ajax
        $.ajax({
            url : MODULE_PATH + 'basedata.campus/edit',
            type : 'POST',
            dataType : 'json',
            data : data,
            success : function (res) {
                if (res.code == 200) {
                    //成功提示
                    layer.msg(res.msg,{
                        shade: 0.4,
                        icon: 1,
                        offset:['50%'],
                        time: 1000 //1秒关闭（如果不配置，默认是3秒）
                    });
                } else {
                    //失败提示
                    layer.msg(res.msg,{
                        shade: 0.4,
                        icon: 2,
                        offset:['50%'],
                        time: 1000 //1秒关闭（如果不配置，默认是3秒）
                    });
                }
            }
        });
    });

    /*=========================== form表单的点击事件监听 ===========================*/
    //增加操作
    window.add = function () {
        layer.open({
            type: 2,
            area: ['500px', '450px'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: true,
            shade:0.4,
            title: '校区添加',
            btnAlign: 'c',
            content: MODULE_PATH + 'basedata.campus/add',  //url 为子布局的url路径
            success:function (layero,index) {
                var iframe = window['layui-layer-iframe' + index];
                iframe.add();
            }
        });
    }


    //修改
    window.edit = function(obj) {
        //修改弹框显示
        layer.open({
            type: 2,
            area:['500px', '450px'],
            fix: false, //不固定
            maxmin: true,
            shadeClose: true,
            shade:0.4,
            title: '校区更改',
            btnAlign: 'c',
            content: MODULE_PATH + 'basedata.campus/edit?id=' + obj.data.id,  //url 为子布局的url路径
            success:function (layero,index) {
                var iframe = window['layui-layer-iframe' + index];
                iframe.edit(obj.data.id);
            }
        });
    }

    //删除
    window.remove = function(obj) {
        layer.confirm('确定要删除该用户', {
            icon: 3,
            title: '提示'
        }, function(index) {
            layer.close(index);
            let  loading = layer.load();
            $.ajax({
                url: MODULE_PATH + "basedata.campus/del",
                dataType: 'json',
                type: 'POST',
                data: {
                    id : obj.data['id']
                },
                success: function(res) {
                    layer.close(loading);
                    if (res.code == 200) {
                        //成功提示
                        layer.msg(res.msg, {
                            icon: 1,
                            shade: 0.1,
                            time: 1000
                        },function () {
                            //数据刷新
                            table.reload('campus_table');
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 2,
                            shade: 0.1,
                            time: 1000
                        });
                    }
                }
            })
        });
    }

    //批量删除
    window.batchRemove = function(obj) {
        let  data = table.checkStatus(obj.config.id).data;
        if (data.length === 0) {
            layer.msg("未选中数据", {
                icon: 3,
                shade: 0.1,
                time: 1000
            });
            return false;
        }
        let  ids = [];
        for (let  i = 0; i < data.length; i++) {
            ids.push(data[i].id);
        }
        layer.confirm('确定要删除这些用户', {
            icon: 3,
            shade: 0.1,
            title: '提示'
        }, function(index) {
            layer.close(index);
            let  loading = layer.load();
            $.ajax({
                url: MODULE_PATH + "basedata.campus/del",
                dataType: 'json',
                type: 'POST',
                data: {
                    'id' : ids
                },
                success: function(res) {
                    layer.close(loading);
                    if (res.code == 200) {
                        layer.msg(res.msg, {
                            icon: 1,
                            shade: 0.1,
                            time: 1000
                        }, function() {
                            table.reload('campus_table');
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 2,
                            shade: 0.1,
                            time: 1000
                        });
                    }
                }
            })
        });
    }

    //重置
    window.refresh = function(param) {
        table.reload('campus_table');
    }
});

//结果提示
function result(code,msg) {
    layui.use(['table','layer'],() => {
        let  table = layui.table
        layer = layui.layer;
        if (code == 200) {
            //成功提示
            layer.msg(msg, {
                shade: 0.4,
                icon: 1,
                time: 1000
            });
            //数据刷新
            table.reload('campus_table');
        } else {
            //失败提示
            layer.msg(msg, {
                shade: 0.4,
                icon: 2,
                time: 1000
            });
        }
    });
}